package org.neframework.webtest.action.location;

import java.util.List;
import java.util.Map;

import org.neframework.webtest.model.t_fk_auth;
import org.neframework.webtest.model.t_fk_location;
import org.neframework.webtest.service.LocationService;
import org.neframework.webtest.util.tree.TreeTools;
import org.neframework.webtest.base.BaseController;

import com.tmsps.neframework.core.utils.ChkTools;
import com.tmsps.neframework.core.utils.JsonTools;
import com.tmsps.neframework.mvc.annotation.Controller;
import com.tmsps.neframework.mvc.annotation.RequestMapping;
import com.tmsps.neframework.mvc.annotation.ResponseBody;
import com.tmsps.neframework.mvc.plugin.Tip;

@Controller
@RequestMapping("/cp/location")
public class LocationController extends BaseController {

	private static final LocationService locService = new LocationService();

	@RequestMapping("/tree")
	public String tree() {
		return "/jsp/location/tree.jsp";
	}

	@RequestMapping("/loc_data")
	@ResponseBody
	public String loc_data() {

		List<Map<String, Object>> list = locService.selectLocationList();

		return TreeTools.turnListToTree(list);
	}

	@RequestMapping("/add")
	@ResponseBody
	public void add(t_fk_location loc, String up_code) {
		loc.setCode(up_code + loc.getCode());
		oc.saveObj(loc);

		this.setTipMsg(true, "保存成功!", Tip.Type.success);
	}

	@RequestMapping("/edit_form")
	@ResponseBody
	public String edit_form(String kid) {
		t_fk_location loc = (t_fk_location) oc.findObjById(kid, t_fk_location.class);
		return JsonTools.toJson("loc", loc);
	}

	@RequestMapping("/edit")
	@ResponseBody
	public void edit(t_fk_location loc) {
		t_fk_location locDb = (t_fk_location) oc.findObjById(loc.getKid(), t_fk_location.class);
		locDb.setName(loc.getName());
		locDb.setCode(loc.getCode());
		locDb.setType(loc.getType());
		oc.updateObj(locDb);

		this.setTipMsg(true, "保存成功!", Tip.Type.success);
	}

	@RequestMapping("/del")
	@ResponseBody
	public void del(String kid) {
		oc.delObjById(kid, t_fk_location.class);

		this.setTipMsg(true, "删除成功!", Tip.Type.success);
	}

	@RequestMapping("/auth_unique")
	@ResponseBody
	public void auth_unique(String kid, String code) {
		t_fk_location loc = null;

		if (ChkTools.isNull(kid)) {
			loc = locService.findLocByCode(code);
		} else {
			loc = locService.findLocByCode(kid, code);
		}

		if (loc == null) {
			result.put("success", "true");
		} else {
			result.put("fail", "true");
		}
	}
}
